<?php

use yii\helpers\Html;
use yii\grid\GridView;
use yii\widgets\Pjax;
use yii\widgets\ActiveForm;
use kartik\widgets\DatePicker;

//use mdm\admin\components\AccessControl;
/* @var $this yii\web\View */
/* @var $searchModel ua_backend\models\search\UserSearch */
/* @var $dataProvider yii\data\ActiveDataProvider */

$this->title = '用户管理';

$this->params['breadcrumbs'][] = $this->title;

?>
    <div class="box-header with-border no-padding">
        <div class="search-box flex flex-end no-padding">
            <?php $form = ActiveForm::begin([
                'action' => ['index'],
                'method' => 'get',
                'id' => 'search-form',
                'options' => ['class' => 'form-horizontal'],
            ]); ?>

            <div class='input-group input-group-sm' style='width: 300px;margin-top:5px;'>
                <?= $form->field($searchModel, 'skey',[
                    'options'=>['class'=>'input-group input-group-sm','style'=>'width: 300px;'],
                    'inputOptions' => ['placeholder' => Yii::t('app', 'Search Keyword'),'class' => 'form-control pull-right'],
                ])->label(false); ?>
                <div class="input-group-btn">
                    <button class="btn btn-default" type="submit">
                        <i class="fa fa-search"></i>
                    </button>
                </div>
            </div>

            <?php ActiveForm::end(); ?>
        </div>
    </div>

    <div class="space10"></div>

    <div class="box">
        <?php Pjax::begin(); ?>

        <?= GridView::widget([
            'dataProvider' => $dataProvider,
            'layout' => '<div class="box-body">{items}</div>
                         <div class="box-footer">
                            {summary}
                            <div class="_pager">{pager}</div>
                         </div>',

            'columns' => [
                'id' => [
                    'attribute' => 'id',
                    'enableSorting' => false,
                    'label' => 'UID',
                ],

                [
                    'attribute' => 'avatar',
                    'format' => ['image',['width'=>'40','height'=>'40']],
                    'label' => Yii::t('app', 'Avatar'),
                    'enableSorting' => false,
                    'value' => function ($model){
                        if(substr($model->avatar,0,4)=='http'){
                            return $model->avatar;
                        }else{
                            return FILE_PATH.$model->avatar;
                        }
                    }
                ],

                [
                    'attribute' => 'nickname',
                    'label' => Yii::t('app', 'Nickname'),
                    'enableSorting' => false,
                    'contentOptions' => ['width'=>'15%'],
                    'value' => function ($model) {
                        try {
                            if(!json_decode($model->nickname)){
                                throw new Exception();
                            }else{
                                $nickname = json_decode($model->nickname);
                            }
                        }catch (Exception $e){
                            $nickname = urldecode($model->nickname);
                        }
                        return $nickname;
                    }
                ],

                [
                    'attribute' => 'created_at',
                    'label' => Yii::t('app', 'Created at'),
                    'format' => ['date', 'php:Y-m-d'],
                    'enableSorting' => false,
                ],


//                [
//                    'attribute' => 'status',
//                    'enableSorting' => false,
//                    'label' => Yii::t('app', 'Status'),
//                    'format' => 'html',
////                    'visible'=>(Yii::$app->user->id === 1), //仅对某些用户展示
//                    'value' => function ($model) {
//                        $html = '/';
//                        if($model->status == 2){
//                            $html =  '<span class="label label-success">已核销</span>';
//                        }else{
//                            if($model->giftcode!=''){
//                                $html = '<span class="label label-danger">待核销</span>';
//                            }
//                        }
//                        return $html;
//                    }
//                ],


                [
                    'header' => Yii::t('app', 'Operations'),
                    'class' => 'ua_backend\components\ActionColumn',
//                    'visible'=>(Yii::$app->user->id === 1),
                    'template' => '{gift}',
                    'buttons' => [
                        'gift' => function($url, $model){
                            return Html::a('<span class="fs-16 fa fa-gift"></span><span
            class="fs-13 pd-lr-5">赠送词包</span>', '/order/create?uid='.$model->id);
                        },
                    ]
                ],
            ],
        ]); ?>
        <?php Pjax::end(); ?>
    </div>

<?php
//记录当前页面
$url = Yii::$app->request->getHostInfo().Yii::$app->request->url;
Yii::$app->user->setReturnUrl($url);
?>
